package com.cn.xiaonuo.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ExchangeRateUtils {

    public static void main(String[] args) {
        String host = "https://jisuhuilv.market.alicloudapi.com";
        String path = "/exchange/convert";
        String method = "ANY";
        String appcode = "0f9b9edee8ed469aa5de5b3c26ae386d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("amount", "5");
        querys.put("from", "CNY");
        querys.put("to", "HKD");
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, "post", headers, querys, "");
            String s = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(s);
            String msg = (String)jsonObject.get("msg");
            if("ok".equals(msg)){
                Map result =(Map) jsonObject.get("result");
                BigDecimal rate = com.cn.xiaonuo.common.utils.GetJavaTypeConvert.getBigDecimal(result.get("rate"));
                BigDecimal camount = com.cn.xiaonuo.common.utils.GetJavaTypeConvert.getBigDecimal(result.get("camount"));
                System.err.println(rate);
                System.err.println(camount);
            }

            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static JSONObject exchangeHL(String amount,
                                        String from,
                                        String to
                           ) {
        Map<String, Object> resultMap = new HashMap<>();
        String host = "https://jisuhuilv.market.alicloudapi.com";
        String path = "/exchange/convert";
        String method = "ANY";
        String appcode = "0f9b9edee8ed469aa5de5b3c26ae386d";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("amount", amount);//数量
        querys.put("from", from);//要换算的单位（所有货币接口中获取）
        querys.put("to", to);//换算后的单位（所有货币接口中获取）
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, "post", headers, querys, "");
            String s = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(s);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
